package sync

import (
	"context"
	erp "ez/apps/erp/document"
	"ez/apps/syncs/document"
	"gitee.com/dreamwood/ez-go/ez"
	sync "gitee.com/dreamwood/ez-go/model-sync"
)

func init() {
	ez.Subscribe(ez.EventAfterServerRun, func(v interface{}, ctx context.Context) {
		ez.Try(sync.GetMS().SubCrud("ez.v1.core", "User", "Emp", func(data *sync.ModelData) error {
			switch data.Crud {
			case sync.CRUD_D:
				//删除行为
				emp := new(erp.Emp)
				ez.Try(data.FillModel(&emp))
				emp.SetEvent(false)
				ez.Try(emp.Delete())
			case sync.CRUD_C:
				user := new(syncs.User)
				emp := new(erp.Emp)
				ez.Try(data.FillModel(&user))
				emp.Id = user.Id
				ez.Try(emp.GetFactory().Update(emp, user))

			case sync.CRUD_U:
				user := new(syncs.User)
				emp := new(erp.Emp)
				ez.Try(data.FillModel(&user))
				emp.Id = user.Id
				ez.Try(emp.GetFactory().Update(emp, user))
			}
			return nil
		}))
	})
}
